package com.xxd.rpt3.controller;

import com.xxd.rpt3.Entity.JwtTokenHis;
import com.xxd.rpt3.Entity.User;
import com.xxd.rpt3.service.DepartmentService;
import com.xxd.rpt3.service.UserService;
import com.xxd.utils.EHCacheUtils;
import com.xxd.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "tags:测试", value = "value：测试")
@RestController
@RequestMapping("/testSwagger")
class TestSwagger {

	@Autowired
	UserService userService;
	@Autowired
	DepartmentService departmentService;

	@ApiOperation(value = "value:测试swagger", notes = "notes:备注")
	@ApiImplicitParams(value = { @ApiImplicitParam(name = "name", value = "用户名", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "id", value = "用户ID", dataType = "String", paramType = "query") })
	@RequestMapping(value = "test", method = RequestMethod.GET)
	public String test(@RequestParam(name = "name", required = false) String name, @RequestParam(required = false) Integer id) {

		return id + ":" + name;
	}

	@RequestMapping(value = "test1", method = RequestMethod.GET)
	public Object test1(HttpServletRequest request) {
		JwtTokenHis jwtTokenHis =(JwtTokenHis) EHCacheUtils.getTokenCache((String) request.getAttribute(JwtUtil.TOKEN_NAME));
		return jwtTokenHis.getValue();
	}

	@RequestMapping(value = "getPageUser", method = { RequestMethod.POST, RequestMethod.GET })
	public Object getPageUser(@RequestParam(required = false, defaultValue = "管理") String name, @RequestParam(required = false, defaultValue = "6") int deptId,
			@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int pageSize) {
		User user = new User();
		user.setName(name);
		user.setDepartment(departmentService.getById(deptId));

		return userService.findByPage(user, page, pageSize);
	}

	@RequestMapping(value = "getAllDepartment", method = { RequestMethod.POST, RequestMethod.GET })
	public Object getAllDepartment() {
		return departmentService.findAll();
	}
}
